﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using qltscd.Models;
using qltscd.Models.ScreenClient;
using qltscd.Contansts;
using Telerik.Web.Mvc;
using qltscd.Helper;
using qltscd.Views.DanhMucThietBi.Models;
using System.Text;
using System.Web.Routing;

namespace qltscd.Controllers
{
    public class DoiMatKhauController : BaseController
    {
        protected override void OnActionExecuting (ActionExecutingContext filterContext) {
            base.OnActionExecuting(filterContext);
            if (SessionHelper.QuyenNhom[qltscd.Contansts.KeyContansts.QuyenHeThong] == '0') {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new {
                    controller = "KhongCoQuyen",
                    action = "Index"
                }));
            }
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult DoiMatKhau(string Matkhaucu, string Matkhaumoi, string reMatkhaumoi)
        {

            QLTBEntities db = new QLTBEntities();
                DanhMucNguoiDung user = db.DanhMucNguoiDungs.Single(i => i.IdentityNguoiDung == SessionHelper.UserId.Value);
                if (Matkhaucu.Trim().Length > 0 && Matkhaumoi.Trim().Length > 0 && reMatkhaumoi.Trim().Length > 0)
                {
                    if (user.MatKhau.Trim() == Matkhaucu.Trim())
                    {

                        if (Matkhaumoi != reMatkhaumoi)
                        {
                            Session["ErrorMsg"] = "Mật khẩu xác nhận chưa chính xác";
                            return RedirectToAction("Index");
                        }
                        user.MatKhau = Matkhaumoi;
                        db.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);
                        db.SaveChanges();
                        Session["ErrorMsg"] = "";
                        return RedirectToAction("Index","DangNhap");
                    }
                    else
                        Session["ErrorMsg"] = "Sai mật khẩu cũ";
                    return RedirectToAction("Index");
                }
                else
                    Session["ErrorMsg"] = "Không Được Để Trống";
                return RedirectToAction("Index");
        }
    }
  
}

